package com.huawei.chengdu.meetingdemo.view;

import android.app.Presentation;
import android.content.Context;
import android.os.Bundle;
import android.view.Display;
import android.view.SurfaceView;
import android.view.View;

import com.device.idea.meeting.MultiStream.model.LayoutInfo;
import com.device.idea.meeting.MultiStream.model.LayoutSubWindow;
import com.device.idea.meeting.MultiStream.model.StreamType;
import com.device.idea.meeting.MultiStream.model.SurfaceViewInfo;
import com.huawei.chengdu.meetingdemo.R;
import com.huawei.chengdu.meetingdemo.presenter.ConfSdkPresenter;

import java.util.ArrayList;
import java.util.List;

public class MeetingPresentation extends Presentation {
    private final List<SurfaceViewInfo> surfaceViewInfos = new ArrayList<>();
    public SurfaceView surfacePresentation;
    private LayoutInfo layoutInfo;
    public StreamType currentShowType;

    public MeetingPresentation(Context outerContext, Display display, LayoutInfo layoutInfo) {
        super(outerContext, display);
        this.layoutInfo = layoutInfo;
    }

    public MeetingPresentation(Context outerContext, Display display) {
        super(outerContext, display);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.meeting_presentation);
        surfacePresentation = findViewById(R.id.surface_presentation);
        if (layoutInfo != null) {
            handleLayoutChangeNotify();
        }
    }

    public void handleLayoutChangeNotify() {
        surfaceViewInfos.clear();
        LayoutSubWindow subWindow = layoutInfo.getWindowsInfo().getSubWindows().get(0);
        SurfaceViewInfo surfaceViewInfo = bindSurface(subWindow, surfacePresentation);
        surfaceViewInfos.add(surfaceViewInfo);
        ConfSdkPresenter.confirmViewCreateSuccess(surfaceViewInfos);
    }

    public SurfaceViewInfo bindSurface(LayoutSubWindow subWindow, SurfaceView surface) {
        SurfaceViewInfo surfaceViewInfo = new SurfaceViewInfo();
        surfaceViewInfo.setViewId(subWindow.getViewId());
        surfaceViewInfo.setSurfaceView(surface);
        return surfaceViewInfo;
    }

    public void setLayoutInfo(LayoutInfo layoutInfo) {
        this.layoutInfo = layoutInfo;
    }

    public void startShowView(StreamType streamType) {
        if (currentShowType != null) {
            InMeetingFragmentFlexible.mHandler.sendEmptyMessage(currentShowType.getVal());
        }
        surfacePresentation.setVisibility(View.VISIBLE);
        currentShowType = streamType;
    }

    public void stopShowView() {
        surfacePresentation.setVisibility(View.GONE);
        currentShowType = null;
    }
}
